//
//  DestinationDetailScrollView.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/25.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "DestinationDetailScrollView.h"

@implementation DestinationDetailScrollView
-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //        [self createSubViews];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
    
}

-(void)createSubViews:(DestinationDetail *)destinantionDetail andArray:(NSMutableArray *)array {
    self.destinationDetail = destinantionDetail;
    UIImageView *imgaeV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    [imgaeV sd_setImageWithURL:[NSURL URLWithString:destinantionDetail.shr_image] placeholderImage:[UIImage imageNamed:@"icon1024.png"]];
//    imgaeV.backgroundColor = [UIColor redColor];
    [self addSubview:imgaeV];

    
    
    BaseLabel *nameL = [[BaseLabel alloc] initWithFrame:CGRectMake(0, imgaeV.frame.size.height + 10, WIDTH, 30)];
    nameL.text = destinantionDetail.name;
    nameL.textAlignment = NSTextAlignmentCenter;
    nameL.font = [UIFont boldSystemFontOfSize:20];
    [self addSubview:nameL];
//    nameL.backgroundColor = [UIColor greenColor];
    
    BaseLabel *recL = [[BaseLabel alloc] initWithFrame:CGRectMake(0, nameL.frame.size.height + nameL.frame.origin.y + 5, WIDTH, 30)];
    recL.textAlignment = NSTextAlignmentCenter;
    recL.font = [UIFont systemFontOfSize:15];
    NSString *str = [NSString stringWithFormat:@"< %@ 人去过 || %@ 人喜欢 >", destinantionDetail.visited_count, destinantionDetail.wish_to_go_count];
    recL.text = str;
    [self addSubview:recL];

    for (int i = 0; i < array.count; i++) {
        
        UIImageView *imageV = [[UIImageView alloc] init];
//        imageV.backgroundColor = [UIColor redColor];
        [self addSubview:imageV];
        imageV.frame =CGRectMake(20 + i % 2 * ((WIDTH - 60) / 2 + 23), (recL.frame.origin.y + recL.frame.size.height + i / 2 * ((WIDTH - 60) / 2 + 30)), (WIDTH - 60) / 2, (WIDTH - 60) / 2);
        [self addSubview:imageV];
        NSString *str = array[i];

        [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"icon1024.png"]];
//        imageV.backgroundColor = [UIColor redColor];
    }
    NSInteger num = 0;
    if (array.count % 2 == 0) {
        num = array.count / 2;
    } else {
        num = array.count / 2 + 1;
    }
    
    
    self.contentSize = CGSizeMake(0, recL.frame.origin.y + recL.frame.size.height + (((WIDTH - 60) / 2 + 30) * num + 40));
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, recL.frame.origin.y + recL.frame.size.height + (((WIDTH - 60) / 2 + 30) * num), WIDTH - 60, 30)];
    [button setTitle:@"查看更多" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor lightGrayColor];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 10;
    [self addSubview:button];
}

-(void)buttonAction:(UIButton *)button {

    [self.myDelegate sendDestinationModle:self.destinationDetail];
    
    
}

-(CGFloat)heightCustom:(NSString *)content {
    //参数1.设置计算内容的宽度
    //参数2.设置计算高度
    //参数3.设置字体大小属性
    //参数4.系统备用参数,设置为nil
    
    //其中宽度一定与消失内容的label宽度一致,否则不准确
    CGSize size = CGSizeMake(WIDTH, 100000);
    
    //字体大小一定与显示内容的label字体大小一致,默认 17
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    
    return frame.size.height;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
